package priv.winnine.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import priv.winnine.annotation.CommonCheck;
import priv.winnine.entity.enums.ResultEnum;
import priv.winnine.exception.ResultException;
import priv.winnine.util.ResultUtil;

@Aspect
@Component
@Slf4j
public class CheckAspect {

    @Around("@annotation(priv.winnine.annotation.CommonCheck)")
    public Object doCheck(ProceedingJoinPoint pjp) throws Throwable {
        try {
            MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
            CommonCheck handle = methodSignature.getMethod().getAnnotation(CommonCheck.class);
            if (handle.checkDate()) {
                log.info("checkDate()");
                // check
                // TODO
                // ResultUtil.result(ResultEnum.FAIL);
            }
            if (handle.checkTime()) {
                log.info("checkTime()");
                // check
                // TODO
                // ResultUtil.result(ResultEnum.FAIL);
            }
        } catch (ResultException re) {
            return ResultUtil.result(ResultEnum.FAIL);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultUtil.result(ResultEnum.FAIL);
        }
        return pjp.proceed();
    }

}
